<template>
  <div class="p-2">
    <!--引用表格-->
    <BasicTable @register="registerTable" :rowSelection="rowSelection">
      <!--插槽:table标题-->
      <template #tableTitle>
        <a-space>
          <a-input v-model:value="nameValue" style="border-radius: 5px;" placeholder="镜像名称"/>
          <a-input v-model:value="hubUrl" style="border-radius: 5px;" placeholder="镜像地址"/>
          <a-button type="primary" style="border-radius: 5px;" @click="handleSearch">搜索</a-button>
          <a-button type="primary" shape="round" @click="handleAdd" preIcon="ant-design:plus-outlined">新增</a-button>
        </a-space>
        <a-dropdown v-if="selectedRowKeys.length > 0">
          <template #overlay>
            <a-menu>
              <a-menu-item key="1" @click="batchHandleDelete">
                <Icon icon="ant-design:delete-outlined"></Icon>
                删除
              </a-menu-item>
            </a-menu>
          </template>
          <a-button>批量操作
            <Icon icon="mdi:chevron-down"></Icon>
          </a-button>
        </a-dropdown>
      </template>
      <template #bodyCell = "{ column, record }">
        <!-- 对镜像状态做枚举值的设定,分别有待提交,待审核,审核通过,审核不通过 -->
        <a v-if = "column.dataIndex == 'status' ">
          <a-badge v-if="record.imageStatusType == 'default' " status="default" color="blue" :text="record.imageStatusName"/>
          <a-badge v-else-if="record.imageStatusType == 'waiting' " status="processing" color="yellow" :text="record.imageStatusName"/>
          <a-badge v-else-if="record.imageStatusType == 'pass' " status="processing" color="green" :text="record.imageStatusName"/>
          <a-badge v-else-if="record.imageStatusType == 'reject' " color="red" :text="record.imageStatusName"/>
        </a>
      </template>
      <!--操作栏-->
      <template #action="{ record }">
        <!--- 改成用几个醒目的按钮 --->
        <a-space>
          <!--- 当镜像状态为待提交时,显示为提交审核 --->
          <!--- 当镜像状态为待审核时,无显示其他按钮 --->
          <!--- 当镜像状态为审核通过时,显示创建容器 --->
          <!--- 当镜像状态为审核不通过时,显示再次审核 --->
          <a-button v-if="record.imageStatusType == 'pass'" size="small" type="danger" @click="handleDelete(record)">创建容器</a-button>
          <a-button v-else-if="record.imageStatusType == 'default'" shape="round" size="small" type="danger" @click="handleSubmitReview(record)">提交审核</a-button>
          <a-button v-else-if="record.imageStatusType == 'reject'" size="small" type="danger" @click="handleSubmitReview(record)">再次提审</a-button>
          <a-button size="small" type="primary" @click="handleEdit(record)">编辑</a-button>
          <a-button size="small" type="primary" @click="handleDetail(record)">详情</a-button>
          <a-button size="small" type="danger" @click="handleDelete(record)">删除</a-button>
        </a-space>
      </template>
    </BasicTable>
    <!-- 表单区域 -->
    <VmImageModal ref="registerModal" @success="handleSuccess"></VmImageModal>
    <!--- 弹窗 --->
    <BasicDrawer @register="imageRegister" title="查看详情" width="50%">
      <a-descriptions bordered title="镜像信息">
      <a-descriptions-item :span="4" label="镜像ID">{{vmImageData.imageId}}</a-descriptions-item>
      <a-descriptions-item :span="4" label="镜像地址">{{vmImageData.hubUrl}}</a-descriptions-item>
      <a-descriptions-item :span="4" label="暴露端口">{{vmImageData.ports}}</a-descriptions-item>
      <a-descriptions-item :span="4" label="启动指令">{{vmImageData.command}}</a-descriptions-item>
      <a-descriptions-item :span="4" label="镜像描述">{{vmImageData.description}}</a-descriptions-item>
      <a-descriptions-item :span="4" label="默认登录账号">{{vmImageData.sshUsername}}</a-descriptions-item>
      <a-descriptions-item :span="4" label="默认登录密码">{{vmImageData.sshPassword}}</a-descriptions-item>
      <a-descriptions-item :span="4" label="审核人">{{vmImageData.reviewer}}</a-descriptions-item>
    </a-descriptions>
    </BasicDrawer>
  </div>
</template>

<script lang="ts" name="vm-vmImage" setup>
  import { ref, reactive } from 'vue';
  import { BasicTable } from '/@/components/Table';
  import { useListPage } from '/@/hooks/system/useListPage';
  import { columns } from './VmImage.data';
  import { list, deleteOne, batchDelete, getImportUrl, getExportUrl, submitReview } from './VmImage.api';
  import VmImageModal from './components/VmImageModal.vue';
  import { BasicDrawer, useDrawer } from '/@/components/Drawer';

  const nameValue = ref<string>('');
  const hubUrl = ref<string>('');
  function handleSearch() {
    queryParam['name'] = nameValue.value;
    queryParam['hubUrl'] = hubUrl.value;
    reload();
  }

  // 镜像数据弹窗
  const vmImageData = ref<any>();
  const [imageRegister, imageDrawerMethods] = useDrawer();
  const queryParam = reactive<any>({});
  const registerModal = ref();
  //注册table数据
  const {  tableContext } = useListPage({
    tableProps: {
      title: 'vm_image',
      api: list,
      columns,
      canResize:true,
      useSearchForm: false,
      actionColumn: {
        width: 300,
        resizable: true,
        fixed: 'right',
      },
      beforeFetch: (params) => {
        return Object.assign(params, queryParam);
      },
    },
    exportConfig: {
      name: "vm_image",
      url: getExportUrl,
      params: queryParam,
    },
	  importConfig: {
	    url: getImportUrl,
	    success: handleSuccess
	  },
  });
  const [registerTable, { reload }, { rowSelection, selectedRowKeys }] = tableContext;

  /**
   * 新增事件
   */
  function handleAdd() {
    registerModal.value.disableSubmit = false;
    registerModal.value.add();
  }
  /**
   * 提交审核
   */
  async function handleSubmitReview(record: Recordable) {
    await submitReview({ id: record.id},handleSuccess);
  }
  
  /**
   * 编辑事件
   */
  function handleEdit(record: Recordable) {
    registerModal.value.disableSubmit = false;
    registerModal.value.edit(record);
  }
   
  /**
   * 详情
   */
  function handleDetail(record: Recordable) {
    // 设置为弹窗
    console.log("打开了弹窗",record);
    vmImageData.value = record;
    imageDrawerMethods.openDrawer(true,record);
  }
   
  /**
   * 删除事件
   */
  async function handleDelete(record) {
    await deleteOne({ id: record.id }, handleSuccess);
  }
   
  /**
   * 批量删除事件
   */
  async function batchHandleDelete() {
    await batchDelete({ ids: selectedRowKeys.value }, handleSuccess);
  }
   
  /**
   * 成功回调
   */
  function handleSuccess() {
    (selectedRowKeys.value = []) && reload();
  }
   
   

  
  /**
   * 重置
   */
  // function searchReset() {
  //  formRef.value.resetFields();
  //  selectedRowKeys.value = [];
  //  //刷新数据
  //  reload();
  //}
  



</script>

<style lang="less" scoped>
  .jeecg-basic-table-form-container {
    padding: 0;
    .table-page-search-submitButtons {
      display: block;
      margin-bottom: 24px;
      white-space: nowrap;
    }
    .query-group-cust{
      width: calc(50% - 15px);
      min-width: 100px !important;
    }
    .query-group-split-cust{
      width: 30px;
      display: inline-block;
      text-align: center
    }
  }
</style>
